# -*- coding: utf-8 -*-
#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 11/ago/2011

@author: antstud
'''

import wx
from wx.lib.expando import ExpandoTextCtrl, EVT_ETC_LAYOUT_NEEDED

#===============================================================================
# TitoloPanel
#===============================================================================
class TitoloPanel(wx.Panel):
    def __init__(self,*args,**kwargs):
        wx.Panel.__init__(self,*args,**kwargs)
        self.__do_layout()
        
    def __do_layout(self):
        dati_azienda=self._request_dati_azienda()
        mainsizer=wx.BoxSizer(wx.VERTICAL)
        self.SetBackgroundColour('white')
        self.titolo=titolo=wx.StaticText(self,label=str(dati_azienda.ragione_sociale))
        font=titolo.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        font.SetUnderlined(True)
        font.PointSize=20
        titolo.SetFont(font)
        self.sub_titolo=sub_titolo=wx.StaticText(self,label=str(dati_azienda.sottotitolo))
        font=sub_titolo.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        #font.SetUnderlined(True)
        #font.PointSize=20
        sub_titolo.SetFont(font)
        mainsizer.Add(titolo,0,wx.CENTER|wx.LEFT|wx.RIGHT|wx.TOP,border=30)
        mainsizer.Add(sub_titolo,0,wx.CENTER|wx.ALL,border=0)
        mainsizer.AddSpacer(30)
        mainsizer.Layout()
        self.SetSizer(mainsizer)
    
    def _request_dati_azienda(self):
        observer=wx.GetApp().get_observer()
        observer.set_context('AZIENDA')
        return observer.get_dati_azienda()
    
    def update(self):
        dati_azienda=self._request_dati_azienda()
        self.titolo.SetLabel(dati_azienda.ragione_sociale)
        self.sub_titolo.SetLabel(dati_azienda.sottotitolo)


#===============================================================================
# AttivitaPanel
#===============================================================================
class AttivitaPanel(wx.Panel):
    def __init__(self,*args,**kwargs):
        wx.Panel.__init__(self,*args,**kwargs)
        self.of_da_concludere=self._request_of_da_concludere()
        self.oc_da_concludere=self._request_oc_da_concludere()
        self.__do_layout()
    
    def __do_layout(self):
        mainsizer=wx.BoxSizer(wx.VERTICAL)
        self.SetBackgroundColour('white')
        titolo=wx.StaticText(self,label='Attività')
        font=titolo.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        titolo.SetFont(font)
        mainsizer.Add(titolo,0,wx.ALL,border=5)
        line=wx.StaticLine(self)
        line.SetBackgroundColour('red')
        mainsizer.Add(line,0,wx.EXPAND)
        stringa='{0}ci sono {1} ordini {2} da concludere'
        non='non '
        num=''
        if len(self.oc_da_concludere):
            non=''
            num=len(self.oc_da_concludere)
        t=wx.StaticText(self,label=stringa.format(non,num,'cliente').capitalize())
        mainsizer.Add(t,0,wx.ALL,border=5)
        
        mainsizer.AddSpacer(15)
        
        non='non '
        num=''
        if len(self.of_da_concludere):
            non=''
            num=len(self.of_da_concludere)
        t=wx.StaticText(self,label=stringa.format(non,num,'fornitore').capitalize())
        mainsizer.Add(t,0,wx.ALL,border=5)
        
        mainsizer.Layout()
        self.SetSizer(mainsizer)
    
    def update(self):
        pass
    
    def _request_of_da_concludere(self):
        #TOTO: sistemare
        observer=wx.GetApp().get_observer()
        observer.set_ordini_fornitore_context()
        return observer.action_request('da_concludere')
    
    def _request_oc_da_concludere(self):
        observer=wx.GetApp().get_observer()
        observer.set_ordini_cliente_context()
        return observer.action_request('da_concludere')


#===============================================================================
# ImpegniPanel
#===============================================================================
class ImpegniPanel(wx.Panel):
    def __init__(self,*args,**kwargs):
        wx.Panel.__init__(self,*args,**kwargs)
        self.scaduti=self._request_scaduti()
        self.oggi=self._request_oggi()
        self.prossimi=self._request_prossimi()
        self.__do_layout()
    
    def __do_layout(self):
        mainsizer=wx.BoxSizer(wx.VERTICAL)
        self.SetBackgroundColour('white')
        titolo=wx.StaticText(self,label='Impegni')
        font=titolo.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        titolo.SetFont(font)
        mainsizer.Add(titolo,0,wx.ALL,border=5)
        line=wx.StaticLine(self)
        line.SetBackgroundColour('red')
        mainsizer.Add(line,0,wx.EXPAND)
        #scaduti
        stringa='Non ci sono impegni scaduti.'
        if self.scaduti:
            stringa='Sono scaduti {0} impegni:'.format(len(self.scaduti))
        t=wx.StaticText(self,label=stringa)
        font=t.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        t.SetFont(font)
        mainsizer.Add(t,0,wx.ALL,border=5)
        for i in self.scaduti:
            stringa="* {0} - {1}".format(i.get_scadenza_fmt(),i.nota)
            t=wx.StaticText(self,label=stringa)
            mainsizer.Add(t,0,wx.ALL,border=5)
        mainsizer.AddSpacer(15)
        #oggi
        stringa='Non ci sono impegni previsti per oggi.'
        if self.oggi:
            stringa='Oggi scadono {0} impegni:'.format(len(self.oggi))
        t=wx.StaticText(self,label=stringa)
        font=t.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        t.SetFont(font)
        mainsizer.Add(t,0,wx.ALL,border=5)
        for i in self.oggi:
            stringa="* {0} - {1}".format(i.data_scadenza,i.nota)
            t=wx.StaticText(self,label=stringa)
            mainsizer.Add(t,0,wx.ALL,border=5)
        mainsizer.AddSpacer(15)
        #prossimi
        stringa='Non sono previsti impegni per i prossimi giorni.'
        if self.prossimi:
            stringa='Sono previsti {0} impegni per i prossimi giorni:'.format(len(self.prossimi))
        t=wx.StaticText(self,label=stringa)
        font=t.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        t.SetFont(font)
        mainsizer.Add(t,0,wx.ALL,border=5)
        for i in self.prossimi:
            stringa="* {0} - {1}".format(i.data_scadenza,i.nota)
            t=wx.StaticText(self,label=stringa)
            mainsizer.Add(t,0,wx.ALL,border=5)
        mainsizer.Layout()
        self.SetSizer(mainsizer)
    
    def update(self):
        pass
    
    def _request_scaduti(self):
        observer=wx.GetApp().get_observer()
        #observer.set_agenda_context()
        return observer.request_action('AGENDA','scaduti',0)
    
    def _request_oggi(self):
        observer=wx.GetApp().get_observer()
        #observer.set_agenda_context()
        return observer.request_action('AGENDA','oggi')
    
    def _request_prossimi(self):
        observer=wx.GetApp().get_observer()
        #observer.set_agenda_context()
        return observer.request_action('AGENDA','prossimi',0)


#===============================================================================
# LavagnaPanel
#===============================================================================
class LavagnaPanel(wx.Panel):
    def __init__(self,*args,**kwargs):
        wx.Panel.__init__(self,*args,**kwargs)
        self.lavagna=self._request_lavagna()
        self.__do_layout()
    
    def __do_layout(self):
        mainsizer=wx.BoxSizer(wx.VERTICAL)
        self.SetBackgroundColour('white')
        titolo=wx.StaticText(self,label='Lavagna')
        font=titolo.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        titolo.SetFont(font)
        mainsizer.Add(titolo,0,wx.ALL,border=5)
        line=wx.StaticLine(self)
        line.SetBackgroundColour('red')
        mainsizer.Add(line,0,wx.EXPAND)
        t=wx.StaticText(self,label='Scrivi liberamente quello che desideri')
        mainsizer.Add(t,0,wx.ALL,border=5)
        
        self.eom = ExpandoTextCtrl(self, size=(250,-1),
                                   value=str(self.lavagna.contenuto))
        self.Bind(EVT_ETC_LAYOUT_NEEDED, self.on_refit, self.eom)
        mainsizer.Add(self.eom,0,wx.ALL|wx.EXPAND,border=5)
        
        mainsizer.Layout()
        self.SetSizer(mainsizer)
        self.Fit()
    
    def on_refit(self,evt):
        self.Parent.Parent.Parent.Fit()
    
    def _request_lavagna(self):        
        observer=wx.GetApp().get_observer()
        observer.set_context('LAVAGNA')
        return observer.get_objects()
    
    def get_contenuto(self):
        self.lavagna.contenuto=self.eom.GetValue()
        return self.lavagna
